package com.ui
{
	import com.engine.AwayEngine;
	import com.engine.SurfaceResize;
	import com.greensock.TweenLite;
	import com.manager.CarManager;
	import com.manager.CarMaterial;
	import com.manager.Vision;
	import com.vo.PartVo;
	
	import flash.geom.Rectangle;
	
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.ResizeEvent;
	import starling.events.Touch;
	import starling.events.TouchPhase;
	import starling.extensions.ClippedSprite;
	
	/**
	 * 底部显示部件的List条
	 */	
	public class PartFace extends SurfaceResize
	{
		private static var menu:PartFace;
		public static function getInstance():PartFace{
			if(menu== null) menu = new PartFace();
			return menu;
		}
		
		private var partDataList:Vector.<PartVo>;//部件数据列表
		private var partItemList:Vector.<ListItem>;//部件条目数组列表
		private var partLayer:ClippedSprite;//部件容器
		private var contain:Sprite;
		
		private var lastBtnImage:Image;//前一个按钮
		private var nextBtnImage:Image;//下一个
		
		private var isMove:Boolean;//开始缓动
		private var sign:int = 4;
		private var record:int;
		
		public function show():void{
			initFace();
		}
		
		private function initFace():void
		{
			if(contain == null){
				contain = new Sprite();
				partLayer = new ClippedSprite();
				partLayer.y = 540;
				partLayer.x = 40;
				contain.addChild(partLayer);
				
//				var sprite:ClippedSprite = new ClippedSprite();
//				contain.addChild(sprite);
//				sprite.addChild(partLayer);
//				sprite.clipRect = new Rectangle(partLayer.x,partLayer.y, 920, 200);
				partLayer.clipRect = new Rectangle(partLayer.x,partLayer.y, 920, 200);
				
				createBigArrows();
				loaderSlogan();
				
				contain.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			}
			Vision.addView(Vision.UI,contain);
		}
		private function addToStage(e:Event):void
		{
			contain.removeEventListener(ResizeEvent.RESIZE,addToStage);
			resize();
		}
		
		protected override function resize():void
		{
			contain.x = AwayEngine.senceWidth / 2 - contain.width / 2;
			partLayer.clipRect.x = partLayer.x + contain.x;
		}
		
		private function createBigArrows():void{
			lastBtnImage = CustomSkin.getImage("BigArrows");
//			lastBtnImage.x = 225;
			lastBtnImage.y = 565;
			contain.addChild(lastBtnImage);
			nextBtnImage = CustomSkin.getImage("BigArrows");
			nextBtnImage.x = 1218 - 225;
			nextBtnImage.y = 560;
			nextBtnImage.scaleX = -1;
			contain.addChild(nextBtnImage);
			onLastOpen();
		}
		
		private function loaderSlogan():void{
			var slogan:Image = CustomSkin.getImage("EffectMap");
			slogan.y = 498;
			contain.addChild(slogan);
			var line:Image = CustomSkin.introduce(990,1,0,528,0);
			contain.addChild(line);
		}
		
		private function onLastOpen():void{
			nextBtnImage.addEventListener(starling.events.TouchEvent.TOUCH,onNextBtnImage);
			lastBtnImage.addEventListener(starling.events.TouchEvent.TOUCH,onLastBtnImage);
		}
		private function onLastClose():void{
			nextBtnImage.removeEventListener(starling.events.TouchEvent.TOUCH,onNextBtnImage);
			lastBtnImage.removeEventListener(starling.events.TouchEvent.TOUCH,onLastBtnImage);
		}
		private function onNextBtnImage(e:starling.events.TouchEvent):void{
			if(partItemList == null || partItemList.length == 0)return;
			var touch:Touch = e.getTouch(e.target as Image);
			if(touch!=null){
				if(touch.phase == TouchPhase.BEGAN){
					var num:int = 9*104;
					if(partLayer.x < 266){
						if(partLayer.x > -num&&partLayer.x < 266){
							TweenLite.to(partLayer,.5,{x:266});
							sign = 4;
						}else{
							TweenLite.to(partLayer,.5,{x:partLayer.x + num});
							sign = sign - 9;
						}
					}
				}
			}
		}
		private function onLastBtnImage(e:starling.events.TouchEvent):void{			
			if(partItemList == null || partItemList.length == 0)return;
			var touch:Touch = e.getTouch(e.target as Image);
			if(touch!=null){
				if(touch.phase == TouchPhase.BEGAN){
					var num:int = 9*104;
					var n:Number = (partItemList.length*104)-(num+266)
					if(partLayer.x >= -n){
						if(partLayer.x <= -n+num+266&&partLayer.x >=-n){
							onLastClose();
							TweenLite.to(partLayer,.5,{x:-n,onComplete:onLastOpen});
							sign = partItemList.length - 5 ;
						}else{
							onLastClose();
							TweenLite.to(partLayer,.5,{x:partLayer.x - num,onComplete:onLastOpen});
							sign += 9;
						}
					}
				}
			}
		}
		
		/**
		 * 刷新部件列表
		 * @param pList
		 */		
		public function partRefresh(pList:Vector.<PartVo>):void{
			if(partLayer == null)return;
			partDataList = pList;
			if(partItemList == null)partItemList = new Vector.<ListItem>();
			partItemList.length = 0;
			clearChild(partLayer);
			if(pList == null)return;//没有数据 无需生成视图
			for (var i:int = 0; i < partDataList.length; i++){
				var lt:ListItem = ListItem.crateItem(partDataList[i]);
				lt.name = i + '';
				lt.x = (84 + 20) * i;
				partLayer.addChild(lt);
				partItemList.push(lt);
			}
			addListeners();
		}
		
		private function clearChild(sp:DisplayObjectContainer):void{
			while(sp.numChildren > 0){
				var child:DisplayObject = sp.getChildAt(0);
				child.dispose();
				sp.removeChild(child);
			}
//			sp.removeChildren();
		}
		
		private function moveStart():void{
			isMove = true;
			partLayer.addEventListener(starling.events.Event.ENTER_FRAME,onFrame);
		}
		private function moveStop():void{
			isMove = false;
			partLayer.removeEventListener(starling.events.Event.ENTER_FRAME,onFrame);
		}
		private function onFrame(e:starling.events.Event):void{
			if(isMove){
				removeListeners();
				moveStop();
				if(record >= 0 && record < partItemList.length){
					var uint:Number = (record - sign) * 104;
					if(record <= 4){
						TweenLite.to(partLayer,.5,{x:266,onComplete:addListeners});
						sign = 4;
					}else if(record >= partItemList.length-5){
						TweenLite.to(partLayer,.5,{x:266-((partItemList.length-9)*104),onComplete:addListeners});
						sign = partItemList.length-5;
					}else{
						TweenLite.to(partLayer,.5,{x:partLayer.x - uint,onComplete:addListeners});
						sign = record;
					}
				}
			}
		}
		private function addListeners():void{
			for (var i:int = 0; i < partItemList.length; i++){
				partItemList[i].addEventListener(starling.events.TouchEvent.TOUCH,onMonitor);
			}
		}
		private function removeListeners():void{
			for (var i:int = 0; i < partItemList.length; i++){
				partItemList[i].removeEventListener(starling.events.TouchEvent.TOUCH,onMonitor);
			}
		}
		private function onMonitor(e:starling.events.TouchEvent):void{
			var touch:Touch = e.getTouch(e.currentTarget as DisplayObject);
			if(touch != null){
				if(touch.phase == TouchPhase.BEGAN){
					var item:ListItem = e.currentTarget as ListItem;
					var index:int = int(item.name);
					record = index;
					var pvo:PartVo = item.pvo;
//					var path:String = pvo.texturePath;//材质地址
					moveStart();
					CarManager.replace(pvo);
					DetailFace.getInstance().showInfo(pvo);
				}
			}
		}
	}
}
import com.manager.Vision;
import com.utils.PowerLoader;
import com.vo.PartVo;

import flash.display.Bitmap;
import flash.utils.Dictionary;

import starling.display.Image;
import starling.display.Sprite;
import starling.text.TextField;
import starling.textures.Texture;
import starling.utils.HAlign;

/**
 * 自定义组件容器
 */
class ListItem extends Sprite{
	private static const listWidth:Number = 85;
	private static const listHeiht:Number = 30;
	
	private var icon:Image;//图标
	private var nameText:TextField;//代号
	private var priceText:TextField;//代号
	
	public var pvo:PartVo;//部件所有的数据
	
	private static var itemGroup:Vector.<ListItem>;
	public static function crateItem(pvo:PartVo):ListItem{
		if(itemGroup == null){
			itemGroup = new Vector.<ListItem>();
		}
		if(itemGroup.length > 0){
			var lt:ListItem = itemGroup.pop();
			lt.change(pvo);
			return lt;
		}
		return new ListItem(pvo);
	}
	
	public function ListItem(pvo:PartVo):void{
		initFace();
		change(pvo);
	}
	
	private function initFace():void
	{
		icon = new Image(Vision.EMPTY);
		
		nameText = Vision.createText('',0,0,0,85,20);
		nameText.y = 80;
		nameText.hAlign = HAlign.CENTER;
		
		priceText = Vision.createText('',0x8b0000,0,0,85,20);
		priceText.y = 100;
		priceText.hAlign = HAlign.CENTER;
		
		addEed();
	}
	
	public function change(pvo:PartVo):void{
		this.pvo = pvo;
		nameText.text = pvo.cName;
		priceText.text = "￥: "+ pvo.price;
		PowerLoader.loadFile(pvo.iconPath,onIconLoad);
	}
	
	override public function dispose():void
	{
		nameText.text = priceText.text = "";
		super.dispose();
		itemGroup.push(this);//回收自己
	}
	
	private function onIconLoad(bmp:Bitmap):void
	{
		icon.texture = Texture.fromBitmapData(bmp.bitmapData);
		icon.width = bmp.width;
		icon.height = bmp.height;
		icon.x = listWidth / 2 - icon.width / 2;
	}
	
	private function addEed():void{
		this.addChild(icon);
		this.addChild(nameText);
		this.addChild(priceText);
	}
}